package codingforgreat.class09;

import java.util.HashMap;

public class Class05_IsStepSum {
    //步长和 给定一个数 判断他是不是某个数的步长和 步长和： 680 = 680 + 68 + 0
    public static boolean isStepSum(int stepSum) {
        int L = 0;
        int R = stepSum;
        int mid = 0;
        int cur = 0;
        while(L <= R){
             mid =  (L + R) / 2;
             cur = stepSum(mid);
             if(cur == stepSum){
                 return true;
             }else
             if(cur < stepSum){
                 L = mid + 1;
             }else{
                 R = mid - 1;
             }
        }
        return false;

    }
    public static int stepSum(int num){
        int ans = 0;
        while (num > 0){
            ans += num;
            num = num / 10;
        }
        return ans;
    }
    // for test
    public static HashMap<Integer, Integer> generateStepSumNumberMap(int numMax) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i <= numMax; i++) {
            map.put(stepSum(i), i);
        }
        return map;
    }

    // for test
    public static void main(String[] args) {
        int max = 1000000;
        int maxStepSum = stepSum(max);
        HashMap<Integer, Integer> ans = generateStepSumNumberMap(max);
        System.out.println("测试开始");
        for (int i = 0; i <= maxStepSum; i++) {
            if (isStepSum(i) ^ ans.containsKey(i)) {
                System.out.println("出错了！");
            }
        }
        System.out.println("测试结束");
    }
}
